<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="QT图片主要有QPixmap、QImage和QPicture三个类，他们的父类均为QPaintDevice。

QPixmap/QBitmap: 针对屏幕进行了优化， 和平台相关, 不能对图片进行修改
QImage: 和平台无关，可以对图片进行修改，可以在线程中绘图
QPicture: 保存绘图的状">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="QT基础7——图片加载及保存">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="QT图片主要有QPixmap、QImage和QPicture三个类，他们的父类均为QPaintDevice。

QPixmap/QBitmap: 针对屏幕进行了优化， 和平台相关, 不能对图片进行修改
QImage: 和平台无关，可以对图片进行修改，可以在线程中绘图
QPicture: 保存绘图的状">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>QT基础7——图片加载及保存 - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>QT基础7——图片加载及保存</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2018-12-31
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        

<a href="/blog/categories/QT/">QT</a>

                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>QT图片主要有QPixmap、QImage和QPicture三个类，他们的父类均为QPaintDevice。</p>
<ul>
<li>QPixmap/QBitmap: 针对屏幕进行了优化， 和平台相关, 不能对图片进行修改</li>
<li>QImage: 和平台无关，可以对图片进行修改，可以在线程中绘图</li>
<li>QPicture: 保存绘图的状态（二进制文件)</li>
</ul>
<h4 id="QPixmap-QBitmap"><a href="#QPixmap-QBitmap" class="headerlink" title="QPixmap/QBitmap"></a>QPixmap/QBitmap</h4><p>QBitmap 是QPixmap的子类，图片类型是黑白图片，可以减少图片的大小。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">QPainter p(this);</span><br><span class="line">	</span><br><span class="line">  //把图片画到当前窗口上</span><br><span class="line">  //方法一</span><br><span class="line">  p.drawPixmap(0, 0, QPixmap(&quot;../Image/bg.png&quot;));</span><br><span class="line">  p.drawPixmap(300, 0, QBitmap(&quot;../Image/bg.png&quot;));</span><br><span class="line">	</span><br><span class="line">  //方法二，先load加载，再drawPixmap</span><br><span class="line">  QPixmap pixmap;</span><br><span class="line">  pixmap.load(&quot;../Image/bg.png&quot;);</span><br><span class="line">  p.drawPixmap(0, 200, pixmap);</span><br><span class="line"></span><br><span class="line">  QBitmap bitmap;</span><br><span class="line">  bitmap.load(&quot;../Image/bg.png&quot;);</span><br><span class="line">  p.drawPixmap(300, 200, bitmap);</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  //内存加载图片并保存（不显示到窗口上）</span><br><span class="line">      QPixmap pixmap(400, 300);</span><br><span class="line">  QPainter p(&amp;pixmap);</span><br><span class="line">  //通过画刷填充颜色</span><br><span class="line">  p.fillRect(0, 0, 400, 300, QBrush(Qt::white));</span><br><span class="line">  //直接用绘图设置填充颜色</span><br><span class="line">  pixmap.fill(Qt::white);</span><br><span class="line">  p.drawPixmap(0, 0, 80, 80, QPixmap(&quot;../Image/bg.png&quot;));</span><br><span class="line">  pixmap.save(&quot;../pixmap.jpg&quot;);</span><br></pre></td></tr></table></figure>
<h4 id="QImage"><a href="#QImage" class="headerlink" title="QImage"></a>QImage</h4><p>QImage与QPixmap使用方法基本相同，不同的地方在于可以获取和修改指定位置相素点的值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">//指定图片大小和像素格式</span><br><span class="line">QImage image(400, 300, QImage::Format_ARGB32);</span><br><span class="line">   //加载图片</span><br><span class="line">   image.load(&quot;../Image/bg.png&quot;);</span><br><span class="line">   QPainter p;</span><br><span class="line">   p.begin(&amp;image);</span><br><span class="line">   p.drawImage(0, 0, image);</span><br><span class="line"></span><br><span class="line">   for(int i = 0; i &lt;  50; i++)</span><br><span class="line">   &#123;</span><br><span class="line">       for(int j = 0; j &lt; 50; j++) &#123;</span><br><span class="line">       //将前50*50的像素点改为绿色</span><br><span class="line">           image.setPixel(QPoint(i, j), qRgb(0, 255, 0));</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">//保存图片</span><br><span class="line">   image.save(&quot;../image.jpg&quot;);</span><br><span class="line">   p.end();</span><br></pre></td></tr></table></figure></p>
<h4 id="QPicture"><a href="#QPicture" class="headerlink" title="QPicture"></a>QPicture</h4><p>QPicture可以将图片保存为二进制文件（系统无法识别），可以通过QT加载打开。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">QPicture picture;</span><br><span class="line">  QPainter p;</span><br><span class="line">  p.begin(&amp;picture);</span><br><span class="line"></span><br><span class="line">  p.drawPixmap(0, 0, QPixmap(&quot;../Image/bg.png&quot;));</span><br><span class="line">  p.drawLine(50, 50, 150, 50);</span><br><span class="line">  p.end();</span><br><span class="line">  </span><br><span class="line">  //必须p.end()之后才能调用save()函数</span><br><span class="line">  picture.save(&quot;../pictuer.png&quot;);</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  //加载保存在本地的picture文件</span><br><span class="line">  QPicture pic;</span><br><span class="line">  pic.load(&quot;../pictuer.png&quot;);</span><br><span class="line">  QPainter p(this);</span><br><span class="line">  p.drawPicture(0, 0, pic);</span><br></pre></td></tr></table></figure></p>
<h4 id="QImage-与-QPixmap相互转换"><a href="#QImage-与-QPixmap相互转换" class="headerlink" title="QImage 与 QPixmap相互转换"></a>QImage 与 QPixmap相互转换</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">QPainter p(this);</span><br><span class="line">    QPixmap pixmap;</span><br><span class="line">    pixmap.load(&quot;../Image/bg.png&quot;);</span><br><span class="line">	</span><br><span class="line">    //pixmap转image</span><br><span class="line">    QImage tempImage = pixmap.toImage();</span><br><span class="line">	//image 转 pixmap</span><br><span class="line">    QPixmap tempPixmap = QPixmap::fromImage(tempImage);</span><br><span class="line"></span><br><span class="line">    p.drawPixmap(0, 0, tempPixmap);</span><br><span class="line">    p.drawImage(300, 0, tempImage);</span><br></pre></td></tr></table></figure>

                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>